package com.higgins.eventtracking.client.health.record.entry;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.FinanceTrackingService;
import com.higgins.eventtracking.client.FinanceTrackingServiceAsync;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;
import com.higgins.eventtracking.client.health.components.HealthOverviewChart;
import com.higgins.eventtracking.client.health.components.HealthOverviewChartListBox;
import com.higgins.eventtracking.client.health.fundamentals.AchievementTab;
import com.higgins.eventtracking.client.health.fundamentals.DigestionTab;
import com.higgins.eventtracking.client.health.fundamentals.FrameOfMindTab;
import com.higgins.eventtracking.client.health.fundamentals.GoalsTab;
import com.higgins.eventtracking.client.health.fundamentals.HolidaysTab;
import com.higgins.eventtracking.client.health.fundamentals.HourlyTab;
import com.higgins.eventtracking.client.health.fundamentals.HygieneTab;
import com.higgins.eventtracking.client.health.fundamentals.NegativeTab;
import com.higgins.eventtracking.client.health.fundamentals.SupportTab;
import com.higgins.eventtracking.client.health.fundamentals.TimetableTab;
import com.higgins.eventtracking.client.health.fundamentals.WellBeingTab;

public class RecordEntryTab extends VerticalPanel	implements GenericTabInterface		{
	private final FinanceTrackingServiceAsync financeTrackingService = GWT
											.create(FinanceTrackingService.class);
	private TabPanel tabPanel = new TabPanel();
	private final Exercise exerciseTab = new Exercise();
	private final Diet dietTab = new Diet();
	private final InventoryTab inventoryTab = new InventoryTab();
	private boolean firstRun = true;
	private Integer tabIndex = null;
	private String tabTitle = null;
	private List<GenericTabInterface> listOfTabs = new ArrayList<GenericTabInterface>();
	
	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}
	@Override
	public void setTabIndex(Integer index) {
		this.tabIndex = index;
	}
	@Override
	public String getTabTitle()	{
		return tabTitle;
	}
	@Override
	public void setTabTitle(String tabTitle)	{
		this.tabTitle = tabTitle;
	}

	public RecordEntryTab()	{	
		
	}
	
	public void addGoalsTab()	{
		addTabs();
	}
	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
		populateTabPanel();
		firstRun = false;
	}
	public void populateTabPanel()	{
		addTabs();
		add(tabPanel);
	}
	private void addTabs()	{
		int tabIndex = 0;
		tabPanel.setSize("200em","100em");
		
		exerciseTab.setTabTitle("Exercise");
		exerciseTab.setTabIndex(tabIndex++);
		exerciseTab.update();
		tabPanel.add(exerciseTab, exerciseTab.getTabTitle());
		listOfTabs.add(exerciseTab);

		dietTab.setTabTitle("Diet");
		dietTab.setTabIndex(tabIndex++);
		tabPanel.add(dietTab, dietTab.getTabTitle());
		listOfTabs.add(dietTab);
		
		inventoryTab.setTabTitle("Inventory");
		inventoryTab.setTabIndex(tabIndex++);
		tabPanel.add(inventoryTab, inventoryTab.getTabTitle());
		listOfTabs.add(inventoryTab);
		
		tabPanel.selectTab(0);
		add(tabPanel);
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
            @Override
            public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
            	int selectedIndex = integerSelectionEvent.getSelectedItem();
            	GenericTabInterface tab = listOfTabs.get(selectedIndex);
            	if (tab!=null)	{
            		tab.update();
            	}
            }
        });
	}

	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		// TODO Auto-generated method stub
		
	}

}
